package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ZjjllistBody struct {
	Page       int   `json:"page"`
	Pagesize   int   `json:"pagesize"`
	Classify   int64 `json:"classify" gorm:"classify"`
	Fanshangid int64 `json:"fanshangid" gorm:"fanshangid"`
	Niudanid   int64 `json:"niudanid" gorm:"niudanid"`
	Number     int64 `json:"number" gorm:"number"`
	State      int64 `json:"state" gorm:"state"`
}
type FanshangzjjllistRBody struct {
	Data       []models.Orderfanshanggood `json:"data"`
	PageNo     int                        `json:"pageNo"`
	PageSize   int                        `json:"pageSize"`
	TotalCount int64                      `json:"totalCount"`
	TotalPage  int64                      `json:"totalPage"`
}

func Fanshangzjjllist(ctx *gin.Context) {
	request := ZjjllistBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	currentPage := request.Page
	pageSize := request.Pagesize
	fanshangid := request.Fanshangid
	state := request.State
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderfanshanggood
	res := data_source.Db.Debug()
	if fanshangid > 0 {
		var orderfanshangs []models.Orderfanshang
		data_source.Db.Where("fanshangid = ?", fanshangid).Find(&orderfanshangs)
		var orderidarr []int64
		for _, o := range orderfanshangs {
			orderidarr = append(orderidarr, o.ID)
		}
		res = res.Where("orderfanshangid in (?)", orderidarr)
	}

	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Orderfanshanggood{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FanshangzjjllistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type NiudanzjjllistRBody struct {
	Data       []models.Orderniudangood `json:"data"`
	PageNo     int                      `json:"pageNo"`
	PageSize   int                      `json:"pageSize"`
	TotalCount int64                    `json:"totalCount"`
	TotalPage  int64                    `json:"totalPage"`
}

func Niudanzjjllist(ctx *gin.Context) {
	request := ZjjllistBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	currentPage := request.Page
	pageSize := request.Pagesize
	niudanid := request.Niudanid
	state := request.State
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderniudangood
	res := data_source.Db
	if niudanid > 0 {
		var orderfanshangs []models.Orderniudan
		data_source.Db.Where("niudanid = ?", niudanid).Find(&orderfanshangs)
		var orderidarr []int64
		for _, o := range orderfanshangs {
			orderidarr = append(orderidarr, o.ID)
		}
		res = res.Where("orderniudanid in (?)", orderidarr)
	}

	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Orderniudangood{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := NiudanzjjllistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
